package com.jxstyle.modular.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jxstyle.modular.entity.Menu;
import com.jxstyle.modular.mapper.MenuMapper;

@Service
public class MenuService extends ServiceImpl<MenuMapper, Menu> {

	public Menu getByCode(String code) {
		QueryWrapper<Menu> wrapper = new QueryWrapper<>();
		wrapper.eq("code", code);
		return super.getOne(wrapper);
	}

	public Menu convertTree(List<Menu> list) {
		Map<Integer, Menu> menuMap = new HashMap<>();
		Menu top = new Menu();
		top.setName("顶级");
		top.setCode("0");
		menuMap.put(top.getId(), top);
		for (Menu menu : list) {
			menuMap.put(menu.getId(), menu);
		}
		for (Menu menu : list) {
			Menu parent = menuMap.get(menu.getPid());
			if (parent != null) {
				parent.addChilds(menu);
			}
		}
		return top;
	}
	public String getParentName(int pid) {
		Menu current = super.getById(pid);
		if (current == null) {
			return "顶级";
		}
		return current.getName();
	}
}
